<?php

class OrderTaskController extends Controller
{

    /*工人端
    * 正在获取项目列表
    *
    */
    public function actionGetWorkingProjects()
    {
        try {
            //参数验证
            $day = Util::getParamNotEmpty("day");

            //检测
            $worker_id = SessionService::getUserIDBySessionid();


            $today = DateUtil::getNowDay();




            //实时项目
            if($day >= $today)
            {
                $list = OrderTaskService::getWorkingProjects($worker_id,$day,$today);


            }
            else
            {
                $list = OrderTaskService::getProjectsWithTaskByDay($worker_id,$day);
            }


            //返回数据
            Util::responseList(count($list), $list);


            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 获取某一天某个项目的任务
    *
    */
    public function actionGetTaskByDay()
    {
        try {
            //参数验证
            $order_id = Util::getParamNotEmpty("order_id");
            $day = Util::getParamNotEmpty("day");

            //检测
            $worker_id = SessionService::getUserIDBySessionid();


            //获取列表
            $searchsql = Util::initSearchSql();

            //设置

            $searchsql = Util::getSerarchStrAdd($searchsql,"worker_id",$worker_id);
            $searchsql = Util::getSerarchStrAdd($searchsql,"work_day",$day);
            $searchsql = Util::getSerarchStrAdd($searchsql,"order_id",$order_id);


            //获取list
            $obj = WorkerTasks::model()->find($searchsql);

            if(empty($obj))
                throw new Exception("该天无提交的任务");


            //返回数据
            Util::responseObj($obj);


            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 添加
    *
    */
    public function actionAdd()
    {

        try {
            //检测
            $worker_id = SessionService::getUserIDBySessionid();

            //参数验证
            $order_id = Util::getParamNotEmpty("order_id");
            $day = Util::getParamNotEmpty("day");
            $content = Util::getParamNotEmpty("content");
            $piclist = Util::getParamNotEmpty("piclist");



            //验证
            $order = OrderList::model()->findByPK($order_id);
            Util::checkExistAndNotDelete($order);

            if($order->status != MsgConst::ORDER_STATUS_WORKING)
                throw new Exception("该项目不在进行中，不能提交任务");

            //当天是否已经有任务
            if(OrderTaskService::hasTaskinDay($worker_id,$order_id,$day))
                throw new Exception("今天已经提交过任务了");

            //添加
            $obj = new WorkerTasks();
            Util::initObjData($obj);


            $obj->order_id = $order_id;
            $obj->worker_id = $worker_id;
            $obj->piclist = $piclist;
            $obj->task_content = $content;
            $obj->work_day=$day;
            Util::initObjData($obj);


            Util::saveObjData($obj);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }


}